package com.bolt.report.engine.fill;

import com.bolt.report.definition.ReportDefinition;
import com.bolt.report.engine.FillElementVisitor;
import com.bolt.report.engine.model.PageDetails;
import com.bolt.report.engine.model.ReportPrint;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.Map;

public class FillContext implements Serializable {

    private ReportDefinition definition;

    private Map<String, FillReturnValue> returnValues;

    private ReportPrint reportPrint;

    private PageDetails currentPage;

    private FillElementVisitor elementVisitor;

    private Integer pageSize;

    private Integer pageNumber;

    private Map<String, Object> parameters;

    public FillContext(ReportDefinition definition, Map<String, FillReturnValue> returnValues, ReportPrint reportPrint,Map<String, Object> parameters) {
        this.definition = definition;
        this.returnValues = returnValues;
        this.reportPrint = reportPrint;
        this.elementVisitor = new CellElementVisitor();
        this.parameters = parameters;
    }

    public ReportDefinition getDefinition() {
        return definition;
    }


    public Map<String, FillReturnValue> getReturnValues() {
        return returnValues;
    }

    public FillReturnValue getReturnValue(String key) {
        return returnValues.get(key);
    }

    public ReportPrint getReportPrint() {
        return reportPrint;
    }

    public PageDetails getCurrentPage() {
        return currentPage;
    }

    public void setPage(PageDetails currentPage) {
        this.currentPage = currentPage;
    }

    public FillElementVisitor getElementVisitor() {
        return elementVisitor;
    }

    public HttpServletRequest getServletRequest(){
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (null != requestAttributes) {
            return ((ServletRequestAttributes) requestAttributes).getRequest();
        } else {
            return null;
        }
    }

    public Map<String, Object> getParameters() {
        return parameters;
    }
}